﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;
using CustomControls.CircularProgressBar;

namespace MallInteractiveMap
{
    public class WaitScreen
    {
        public static Border WaitScreenBorder { get; set; }
        private static bool _isShown;
        public static void Show()
        {
            if (_isShown) return;
            _isShown = true;
            WaitScreenBorder.Child = new CircularProgressBar();
            WaitScreenBorder.Visibility = Visibility.Visible;
            DoEvents();
        }

        public static void Hide()
        {
            WaitScreenBorder.Child = null;
            WaitScreenBorder.Visibility = Visibility.Collapsed;
            _isShown = false;
            DoEvents();
        }

        static void DoEvents()
        {
            var frame = new DispatcherFrame(true);
            Dispatcher.CurrentDispatcher.BeginInvoke
            (
            DispatcherPriority.Background,
            (SendOrPostCallback)delegate(object arg)
            {
                var f = arg as DispatcherFrame;
                f.Continue = false;
            },
            frame
            );
            Dispatcher.PushFrame(frame);
        }
    }
}
